//
//  DiagnosisScreenViewController.m
//  MobileChecker
//
//  Created by Lymons on 15-1-12.
//  Copyright (c) 2015年 Lymons. All rights reserved.
//

#import "DiagnosisScreenViewController.h"
#import "SelfDiagnosisViewModel.h"
#import "TNColorScrollViewController.h"

@interface DiagnosisScreenViewController ()

@property (strong, nonatomic) IBOutlet UIButton *resultOkButton;
@property (strong, nonatomic) IBOutlet UIButton *resultNgButton;
@property (strong, nonatomic) IBOutlet UILabel *headerLabel;
@property (strong, nonatomic) IBOutlet UIButton *lost1PointButton;
@property (strong, nonatomic) IBOutlet UIButton *lost2PointButton;
@property (strong, nonatomic) IBOutlet UIButton *lost3PointButton;

@property (strong, nonatomic) UIView *screenView;

@end

@implementation DiagnosisScreenViewController

- (UIView *)viewForDoubleTap
{
    if (_screenView == nil) {
        TNColorViewData *view1Data = [[TNColorViewData alloc] initWithColor:[UIColor redColor]];
        TNColorViewData *view2Data = [[TNColorViewData alloc] initWithColor:[UIColor greenColor]];
        TNColorViewData *view3Data = [[TNColorViewData alloc] initWithColor:[UIColor blueColor]];
        TNColorViewData *view4Data = [[TNColorViewData alloc] initWithColor:[UIColor blackColor]];
        TNColorViewData *view5Data = [[TNColorViewData alloc] initWithColor:[UIColor whiteColor]];
        NSArray *colorData = @[view1Data, view2Data, view3Data, view4Data, view5Data];
        TNColorScrollViewConfig *config = [[TNColorScrollViewConfig alloc] initWithColorDataObjects:colorData bounces:YES showsHorizontalScrollIndicator:NO showsVerticalScrollIndicator:NO pagingEnabled:YES scrollHorizontal:YES];
        TNColorScrollViewController *vc = [[TNColorScrollViewController alloc] initWithColorScrollViewConfig:config];
        _screenView = vc.view;
    }
    return _screenView;
}

- (void)handleSingleTap:(id)x
{
    NSLog(@"Touch begining");
    [self showFullscreenModalViewWithView:self.screenView animated:YES completion:^{
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(5.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
//            [self handleDoubleTap:x];
        });
    }];
}

- (void)handleDoubleTap:(id)x
{
    NSLog(@"Touch end");
    [self dismissFullscreenModalViewWithView:self.screenView animated:YES completion:NULL];
    [self.viewModel checkDidCompleted];
    
    self.resultOkButton.enabled = YES;
    self.resultNgButton.enabled = YES;
    self.lost1PointButton.enabled = YES;
    self.lost2PointButton.enabled = YES;
    self.lost3PointButton.enabled = YES;
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    self.resultOkButton.enabled = NO;
    self.resultNgButton.enabled = NO;
    self.lost1PointButton.enabled = NO;
    self.lost2PointButton.enabled = NO;
    self.lost3PointButton.enabled = NO;
}

#pragma mark -

- (void)bindViewModel {
    [super bindViewModel];
    
    self.viewModel = [[DiagnosisScreenViewModel alloc] init];
    
    void (^resultBlock)(BOOL) = ^(BOOL result) {
        NSLog(@"Screen is OK : %@", result ? @"YES" : @"NO");
        [self performSegueWithIdentifier:@"ScreenCompleted" sender:self];
    };
    self.resultOkButton.rac_command = [self.viewModel makeCommandWithBlock:resultBlock simple:YES];
    self.resultNgButton.rac_command = [self.viewModel makeCommandWithBlock:resultBlock simple:NO];
    self.lost1PointButton.rac_command = [self.viewModel makeCommandWithBlock:resultBlock detail:DiagnosisCheckResultLost1];
    self.lost2PointButton.rac_command = [self.viewModel makeCommandWithBlock:resultBlock detail:DiagnosisCheckResultLost2];
    self.lost3PointButton.rac_command = [self.viewModel makeCommandWithBlock:resultBlock detail:DiagnosisCheckResultLost3];
    RAC(self, headerLabel.text) = RACObserve(self, viewModel.headerString);
}

@end
